package com.carpark.ejb.Model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Vector;
import javafx.beans.property.SimpleStringProperty;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Krzysztof
 */
@Entity
@Table(name = "cars")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CarsModel.findAll", query = "SELECT c FROM CarsModel c"),
    @NamedQuery(name = "CarsModel.findByCarId", query = "SELECT c FROM CarsModel c WHERE c.carId = :carId"),
    @NamedQuery(name = "CarsModel.findByNumberPlate", query = "SELECT c FROM CarsModel c WHERE c.numberPlate = :numberPlate"),
    @NamedQuery(name = "CarsModel.findByMake", query = "SELECT c FROM CarsModel c WHERE c.make = :make"),
    @NamedQuery(name = "CarsModel.findByModel", query = "SELECT c FROM CarsModel c WHERE c.model = :model"),
    @NamedQuery(name = "CarsModel.findByCarType", query = "SELECT c FROM CarsModel c WHERE c.carType = :carType"),
    @NamedQuery(name = "CarsModel.findByClientId", query = "SELECT DISTINCT c FROM CarsModel c INNER JOIN c.parkingpassesModelCollection d INNER JOIN d.passesrecordsModelCollection f WHERE f.clientId = :clientId"),
    @NamedQuery(name = "CarsModel.findByClientIdAndType", query = "SELECT DISTINCT c FROM CarsModel c INNER JOIN c.parkingpassesModelCollection d INNER JOIN d.passesrecordsModelCollection f WHERE f.clientId = :clientId AND c.carType= :type"),
    @NamedQuery(name = "CarsModel.findByComments", query = "SELECT c FROM CarsModel c WHERE c.comments = :comments")})
public class CarsModel implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "CarId")
    private Integer carId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "NumberPlate")
    private String numberPlate;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "Make")
    private String make;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "Model")
    private String model;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "CarType")
    private String carType;
    @Size(max = 100)
    @Column(name = "Comments")
    private String comments;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "carId")
    private Collection<ParkingpassesModel> parkingpassesModelCollection;

    public CarsModel() {
    }

    public CarsModel(Integer carId) {
        this.carId = carId;
    }

    public CarsModel(Integer carId, String numberPlate, String make, String model, String carType) {
        this.carId = carId;
        this.numberPlate = numberPlate;
        this.make = make;
        this.model = model;
        this.carType = carType;
    }

    public CarsModel(String numberPlate, String make, String model, String carType) {

        this.numberPlate = numberPlate;
        this.make = make;
        this.model = model;
        this.carType = carType;
    }

    public Integer getCarId() {
        return carId;
    }

    public void setCarId(Integer carId) {
        this.carId = carId;
    }

    public String getNumberPlate() {
        return numberPlate;
    }

    public void setNumberPlate(String numberPlate) {
        this.numberPlate = numberPlate;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getCarType() {
        return carType;
    }
    
    public SimpleStringProperty carTypeProperty() {
        return new SimpleStringProperty(getCarTypeInPolish());
    }
    
    public String getCarTypeInPolish() {
        if(carType.equals("Personal")){
            return "Osobowy";
        }
        if(carType.equals("Van")){
            return "Dostawczy";
        }
        if(carType.equals("Motorcycle")){
            return "Motor";
        }
        if(carType.equals("Trailer")){
            return "Przyczepka";
        }
        return "";
    }

    public void setCarType(String carType) {
        this.carType = carType;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    @XmlTransient
    public Collection<ParkingpassesModel> getParkingpassesModelCollection() {
        return parkingpassesModelCollection;
    }

    public void setParkingpassesModelCollection(Collection<ParkingpassesModel> parkingpassesModelCollection) {
        this.parkingpassesModelCollection = parkingpassesModelCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (carId != null ? carId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CarsModel)) {
            return false;
        }
        CarsModel other = (CarsModel) object;
        if ((this.carId == null && other.carId != null) || (this.carId != null && !this.carId.equals(other.carId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return numberPlate;
    }

    public Vector<String> makeStringList() {
        Vector<String> row = new Vector<String>();
        row.add(numberPlate);
        row.add(make);
        row.add(model);
        row.add(carType);
        row.add(comments);

        return row;
    }
    //"Numer rejestracyjny", "Marka", "Model", "Typ pojazdu", "Komentarz"),
    public Object[] renderAsRecord() {
        String plateNumberAsRecord = this.numberPlate != null ? this.numberPlate : "";
        String markAsRecord = this.make != null ? this.make : "";
        String modelAsRecord = this.model != null ? this.model : "";
        String carTypeAsRecord = this.carType != null ? this.carType : "";
        String commentsAsRecord = this.comments != null ? this.comments : "";

        Object[] record = {plateNumberAsRecord, markAsRecord, modelAsRecord, carTypeAsRecord, commentsAsRecord};
        return record;
    }
}
